package com.heyi.nettylong.server.config;

import com.heyi.nettylong.common.util.SpringBeanFactory;
import com.heyi.nettylong.server.listener.ChannelDataListener;
import com.heyi.nettylong.server.typeServer.NettyServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 服务端配置初始化
 * @author dengfeng
 * @date 2022/4/213:55
 */
@Configuration
@EnableConfigurationProperties(ServerProperties.class)
@ConditionalOnProperty(prefix = "netty.server", value = "enabled", matchIfMissing = true)
public class ServerPropertiesConfig {

    @Autowired
    ServerProperties serverProperties;

    @Bean
    public SpringBeanFactory getHandlerContext() {
        return new SpringBeanFactory();
    }

    // 这两个入参是为了强制它们先实例化
    @Bean
    public NettyServer getNettyClient(ChannelDataListener channelDataListener, SpringBeanFactory springBeanFactory){

        NettyServer nettyServer = new NettyServer(serverProperties);
        nettyServer.start();
        System.out.println("--------NettyServer启动线程------"+serverProperties.getHost()+ ":" + serverProperties.getPort());
        return nettyServer;
    }
}
